<template>
  <div class="app-container">
    <div class="app-form">
      <ele-form
        v-model="formData"
        v-bind="formConfig"
        :request-fn="handleRequest"
        @request-success="handleRequestSuccess"
      />
    </div>
  </div>
</template>
<script>
import {
  roompersionsUpdate,
  roompersionsView
} from '@projectName/views/diandi_place/api/room/roompersions'
export default {
  data() {
    return {
      showNum: 1,
      useHourseLis: [],
      houseName: '',
      giveHouse: false,
      formData: {
        type: 1,
        enable_week: 1,
        use_hourse_name: '',
        use_hourse: 0,
        meal_type: 1
      },
      formConfig: {
        formDesc: {
          blocs: {
            type: 'cascader-bloc-store',
            label: '选择楼栋'
          },
          room_id: {
            type: 'input',
            label: '房间ID'
          },
          num: {
            type: 'input',
            label: '容纳人数',
            attrs: {
              type: 'number'
            }
          },
          type: {
            type: 'radio',
            label: '人员类型',
            isOptions: true,
            options: [
              {
                text: '成人',
                value: 0
              },
              {
                text: '儿童',
                value: 1
              }
            ]
          }
        },
        order: []
      }
    }
  },
  created() {
    this.id = this.$route.params.id
    this.getViews()
  },
  methods: {
    getViews() {
      const id = this.id
      roompersionsView(id).then((res) => {
        this.formData = res.data
        console.log(res.data.use_hourse.join(','))
        this.formData.use_hourse = res.data.use_hourse.join(',')
        this.houseName = res.data.hourse_name
        this.showNum = res.data.type
      })
    },
    handleRequest(data) {
      console.log(data)
      if (data.blocs?.length) {
        data.bloc_id = data.blocs[0]
        data.store_id = data.blocs[1]
      }
      roompersionsUpdate(data).then((response) => {
        if (response.code === 200) {
          this.$message.success(response.message)
          this.closePage()
        } else {
          this.$message.error(response.message)
        }
      })
      return Promise.resolve()
    },
    handleRequestSuccess() {},
    showgiveCoupon() {
      this.giveHouse = true
    },
    selsectHouse(row) {
      console.log(row, this.formData.type)
      // if (this.formData.type === 5) {
      //   this.giveHouse = false
      //   this.houseName = row.name
      //   this.$set
      //   this.formData.use_hourse[0] = row.id
      // }
      row.roompersionsUpdate
      const titles = []
      const giveCouponIds = []
      row.forEach((row, index) => {
        giveCouponIds.push(row.id)
        titles.push(row.name)
      })
      this.houseName = titles.join(',')
      this.formData.use_hourse = giveCouponIds.join(',')
      this.giveHouse = false
    },
    closePage() {
      this.$store.dispatch('app/closePage', {
        vm: this,
        fromName: this.$route.name,
        toName: 'diandi-place-room-persion-type-index',
        params: {}
      })
    }
  }
}
</script>
